package edu.ccut.saturn.component.impl.sql;

import java.util.List;
import java.util.Map;

import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.xml.ISaturnElement;

public class IterateItemInfo extends AbstractSqlItemInfo {

	private String propertyStr;

	private String openStr;

	private String closeStr;

	private String conjunction;

	public String getPropertyStr() {
		return propertyStr;
	}

	public void setPropertyStr(String propertyStr) {
		this.propertyStr = propertyStr;
	}

	public String getOpenStr() {
		return openStr;
	}

	public void setOpenStr(String openStr) {
		this.openStr = openStr;
	}

	public String getCloseStr() {
		return closeStr;
	}

	public void setCloseStr(String closeStr) {
		this.closeStr = closeStr;
	}

	public String getConjunction() {
		return conjunction;
	}

	public void setConjunction(String conjunction) {
		this.conjunction = conjunction;
	}

	public IterateItemInfo(String prepend) {
		super(prepend);
	}

	public IterateItemInfo(String prepend, String property, String open,
			String close, String conjunction) {
		super(prepend);
		this.propertyStr = property;
		this.openStr = open;
		this.closeStr = close;
		this.conjunction = conjunction;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String getSubSql(Map<String, Object> param) throws Exception {

		if (this.getPropertyStr() != null) {

			if (param.get(this.getPropertyStr()) != null) {

				if (param.get(this.getPropertyStr()) instanceof List) {
					StringBuffer resultBuffer = new StringBuffer();

					if (getSaturnDataSqlItemInfos() != null
							&& getSaturnDataSqlItemInfos().size() == 1) {

						List<ParameterToken> tokens = ((SqlTextInfo) getSaturnDataSqlItemInfos()
								.get(0)).getTokens();

						for (Object item : (List) param.get(this
								.getPropertyStr())) {

							StringBuffer sqlBuffer = new StringBuffer(
									((SqlTextInfo) getSaturnDataSqlItemInfos()
											.get(0)).getText());

							for (ParameterToken token : tokens) {

								if (!this.propertyStr.equals(token
										.getParamName())) {

									if (param.get(token.getParamName()) != null) {

										token.replace(sqlBuffer, (String) param
												.get(token.getParamName()));
									}

								} else {
									token.replace(sqlBuffer, (String) item);
								}
							}
							resultBuffer.append(" " + this.getConjunction()
									+ " " + sqlBuffer.toString() + " ");
						}
						if (this.getPrepend() != null
								&& !"".equals(this.getPrepend())) {

							resultBuffer.replace(resultBuffer.indexOf(this
									.getConjunction()), resultBuffer
									.indexOf(this.getConjunction())
									+ this.getConjunction().length(), this
									.getPrepend()
									+ " " + this.getOpenStr());

						} else {
							resultBuffer.replace(resultBuffer.indexOf(this
									.getConjunction()), resultBuffer
									.indexOf(this.getConjunction())
									+ this.getConjunction().length(), " "
									+ this.openStr);

						}
						resultBuffer.append(this.getCloseStr());
						return resultBuffer.toString();
					}
				}
			}
		}
		throw new SaturnComponentException("");
	}

	@Override
	public String getType() {
		return "iterate";
	}

	@Override
	public ISaturnElement toSaturnElement() {
		ISaturnElement element = super.toSaturnElement();
		element.addAttribute("open", this.openStr);
		element.addAttribute("close", this.closeStr);
		element.addAttribute("conjunction", this.conjunction);
		return element;
	}
}
